package com.lineage.server.model;

import com.add.L1PcUnlock;
import com.lineage.config.ConfigOther;
import com.lineage.server.datatables.ClanMembersTable;
import com.lineage.server.datatables.lock.ClanEmblemReading;
import com.lineage.server.datatables.lock.ClanReading;
import com.lineage.server.datatables.sql.CharacterTable;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_CharReset;
import com.lineage.server.serverpackets.S_PacketBox;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldClan;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1ClanJoin {
  private static final Log _log = LogFactory.getLog(L1ClanJoin.class);
  
  private static L1ClanJoin _instance;
  
  public static L1ClanJoin getInstance() {
    if (_instance == null)
      _instance = new L1ClanJoin(); 
    return _instance;
  }
  
  public boolean ClanJoin(L1PcInstance pc, L1PcInstance joinPc) {
    try {
      int clan_id = pc.getClanid();
      String clanName = pc.getClanname();
      L1Clan clan = WorldClan.get().getClan(clanName);
      if (clan == null)
        return false; 
      int maxMember = 0;
      int charisma = 0;
      L1PcInstance clanleader = CharacterTable.get().restoreCharacter(clan.getLeaderName());
      if (clanleader == null) {
        System.out.println("取回盟主资料时发生错误。");
        return false;
      } 
      charisma = clanleader.getCha();
      clanleader.getQuest().load();
      boolean lv45quest = false;
      if (clanleader.getLevel() >= 50) {
        maxMember = charisma * 3;
      } else {
        maxMember = charisma * 2;
      } 
      if (ConfigOther.SCLANCOUNT > 0)
        maxMember = ConfigOther.SCLANCOUNT; 
      if (joinPc.getRejoinClanTime() != null && 
        joinPc.getRejoinClanTime().getTime() > System.currentTimeMillis()) {
        int time = (int)(joinPc.getRejoinClanTime().getTime() - System.currentTimeMillis()) / 3600000;
        joinPc.sendPackets((ServerBasePacket)new S_ServerMessage(1925, (new StringBuilder(String.valueOf(time))).toString()));
        return false;
      } 
      if (joinPc.getClanid() == 0) {
        String[] clanMembersName = clan.getAllMembers();
        if (maxMember <= clanMembersName.length) {
          joinPc.sendPackets((ServerBasePacket)new S_ServerMessage(188, pc.getName()));
          return false;
        } 
        L1PcInstance[] onlineClanMember;
        int length = (onlineClanMember = clan.getOnlineClanMember()).length;
        int i = 0;
        while (i < length) {
          L1PcInstance clanMembers = onlineClanMember[i];
          clanMembers.sendPackets((ServerBasePacket)new S_ServerMessage(94, joinPc.getName()));
          i++;
        } 
        joinPc.setClanid(clan_id);
        joinPc.setClanname(clanName);
        joinPc.setClanRank(7);
        joinPc.setClanMemberNotes("");
        joinPc.save();
        joinPc.sendPacketsAll((ServerBasePacket)new S_CharReset(joinPc.getId(), clan.getEmblemId()));
        joinPc.sendPackets((ServerBasePacket)new S_PacketBox(173, pc.getClan().getEmblemStatus()));
        clan.addMemberName(joinPc.getName());
        ClanMembersTable.getInstance().newMember(joinPc);
        joinPc.sendPackets((ServerBasePacket)new S_PacketBox(27, 7, joinPc.getName()));
        joinPc.sendPackets((ServerBasePacket)new S_ServerMessage(95, clanName));
        L1PcUnlock.Pc_Unlock(joinPc);
        joinPc.setPcContribution(0);
        joinPc.setClanContribution(0);
        joinPc.setclanadena(0);
        joinPc.setClanNameContribution("");
      } else {
        joinPc.sendPackets((ServerBasePacket)new S_ServerMessage(89));
      } 
    } catch (Exception e) {
      e.printStackTrace();
    } 
    return true;
  }
  
  private void changeClan(L1PcInstance pc, L1PcInstance joinPc, int maxMember) {
    int clanId = pc.getClanid();
    String clanName = pc.getClanname();
    L1Clan clan = WorldClan.get().getClan(clanName);
    String oldClanName = joinPc.getClanname();
    L1Clan oldClan = WorldClan.get().getClan(oldClanName);
    int oldClanId = joinPc.getClanid();
    if (clan != null && oldClan != null && joinPc.isCrown() && joinPc.getId() == oldClan.getLeaderId()) {
      if (maxMember < (clan.getAllMembers()).length + (oldClan.getAllMembers()).length) {
        joinPc.sendPackets((ServerBasePacket)new S_ServerMessage(188, pc.getName()));
        return;
      } 
      L1PcInstance[] onlineClanMember;
      int n = (onlineClanMember = clan.getOnlineClanMember()).length;
      int i = 0;
      while (i < n) {
        L1PcInstance element = onlineClanMember[i];
        element.sendPackets((ServerBasePacket)new S_ServerMessage(94, joinPc.getName()));
        i++;
      } 
      try {
        pc.setClanRank(4);
        pc.save();
        L1PcUnlock.Pc_Unlock(pc);
        pc.sendPackets((ServerBasePacket)new S_PacketBox(27, 4, pc.getName()));
      } catch (Exception e) {
        _log.error(e.getLocalizedMessage(), e);
      } 
      String[] array;
      n = (array = clan.getAllMembers()).length;
      i = 0;
      while (i < n) {
        String membername = array[i];
        try {
          L1PcInstance clanuser = World.get().getPlayer(membername);
          if (clanuser == null)
            clanuser = CharacterTable.get().restoreCharacter(membername); 
          int memberrank = clanuser.getClanRank();
          switch (memberrank) {
            case 7:
              memberrank = 2;
              break;
            case 8:
              memberrank = 5;
              break;
            case 9:
              memberrank = 6;
              break;
          } 
          clanuser.setClanRank(memberrank);
          clanuser.save();
          if (clanuser.getOnlineStatus() == 1)
            clanuser.sendPackets((ServerBasePacket)new S_PacketBox(27, memberrank, membername)); 
        } catch (Exception e2) {
          e2.printStackTrace();
        } 
        i++;
      } 
      n = (array = oldClan.getAllMembers()).length;
      i = 0;
      while (i < n) {
        String element2 = array[i];
        L1PcInstance oldClanMember = World.get().getPlayer(element2);
        if (oldClanMember != null) {
          ClanMembersTable.getInstance().deleteMember(oldClanMember.getId());
          oldClanMember.setClanid(clanId);
          oldClanMember.setClanname(clanName);
          if (oldClan.getLeaderId() == joinPc.getId()) {
            oldClanMember.setClanRank(3);
            oldClanMember.sendPackets((ServerBasePacket)new S_PacketBox(27, 3, oldClanMember.getName()));
          } else {
            oldClanMember.setClanRank(2);
            oldClanMember.sendPackets((ServerBasePacket)new S_PacketBox(27, 2, oldClanMember.getName()));
          } 
          try {
            oldClanMember.save();
          } catch (Exception e3) {
            _log.error(e3.getLocalizedMessage(), e3);
          } 
          clan.addMemberName(oldClanMember.getName());
          ClanMembersTable.getInstance().newMember(oldClanMember);
          oldClanMember.sendPackets((ServerBasePacket)new S_ServerMessage(95, clanName));
          oldClanMember.sendPacketsAll((ServerBasePacket)new S_CharReset(oldClanMember.getId(), clan.getEmblemId()));
          oldClanMember.sendPackets((ServerBasePacket)new S_PacketBox(173, pc.getClan().getEmblemStatus()));
        } else {
          try {
            L1PcInstance offClanMember = CharacterTable.get().restoreCharacter(element2);
            ClanMembersTable.getInstance().deleteMember(offClanMember.getId());
            offClanMember.setClanid(clanId);
            offClanMember.setClanname(clanName);
            offClanMember.setClanRank(2);
            offClanMember.save();
            clan.addMemberName(offClanMember.getName());
            ClanMembersTable.getInstance().newMember(offClanMember);
          } catch (Exception e3) {
            _log.error(e3.getLocalizedMessage(), e3);
          } 
        } 
        i++;
      } 
      ClanEmblemReading.get().deleteIcon(oldClanId);
      ClanReading.get().deleteClan(oldClanName);
      L1ClanMatching Clan = L1ClanMatching.getInstance();
      Clan.deleteClanMatching(oldClan);
    } 
  }
}
